<template>
  <div class="result-container">
    <!-- 分析结果标题 -->
    <div class="result-header">
      <el-icon class="header-icon"><TrendCharts /></el-icon>
      <h2>分析结果</h2>
      <div class="match-score" v-show="result.matchScore !== undefined && result.matchScore !== null">
        <span class="score-label">职业匹配度</span>
        <div class="score-display">
          <span class="score-number" :style="{ color: getScoreTextColor(result.matchScore) }">
            {{ result.matchScore }}%
          </span>
        </div>
        <el-progress 
          :percentage="result.matchScore" 
          :color="getScoreColor(result.matchScore)"
          :stroke-width="8"
          text-inside
        />
      </div>
    </div>

    <!-- 分析结果内容 -->
    <el-row :gutter="20" class="result-content">
      <!-- 技能关键词 -->
      <el-col :xs="24" :sm="12" :md="12" :lg="6">
        <el-card class="result-card keywords-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <el-icon class="card-icon"><Key /></el-icon>
              <span>技能关键词</span>
            </div>
          </template>
          <div class="keywords-content">
            <el-tag 
              v-for="keyword in result.keywords" 
              :key="keyword"
              class="keyword-tag"
              type="primary"
              effect="light"
            >
              {{ keyword }}
            </el-tag>
            <div v-if="!result.keywords || result.keywords.length === 0" class="empty-content">
              <el-icon><Warning /></el-icon>
              <span>未识别到技能关键词</span>
            </div>
          </div>
        </el-card>
      </el-col>

      <!-- 优势分析 -->
      <el-col :xs="24" :sm="12" :md="12" :lg="6">
        <el-card class="result-card strengths-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <el-icon class="card-icon"><TrophyBase /></el-icon>
              <span>优势分析</span>
            </div>
          </template>
          <div class="content-text">
            <p>{{ result.strengths || '暂无优势分析' }}</p>
          </div>
        </el-card>
      </el-col>

      <!-- 劣势分析 -->
      <el-col :xs="24" :sm="12" :md="12" :lg="6">
        <el-card class="result-card weaknesses-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <el-icon class="card-icon"><Warning /></el-icon>
              <span>待改进项</span>
            </div>
          </template>
          <div class="content-text">
            <p>{{ result.weaknesses || '暂无改进建议' }}</p>
          </div>
        </el-card>
      </el-col>

      <!-- 优化建议 -->
      <el-col :xs="24" :sm="12" :md="12" :lg="6">
        <el-card class="result-card suggestions-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <el-icon class="card-icon"><Promotion /></el-icon>
              <span>优化建议</span>
            </div>
          </template>
          <div class="content-text">
            <p>{{ result.suggestions || '暂无优化建议' }}</p>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 操作按钮 -->
    <div class="result-actions">
      <el-button type="primary" @click="$emit('analyze-again')">
        <el-icon><Refresh /></el-icon>
        重新分析
      </el-button>
      <el-button @click="exportResult">
        <el-icon><Download /></el-icon>
        导出结果
      </el-button>
    </div>
  </div>
</template>

<script>
import { 
  TrendCharts, 
  Key, 
  TrophyBase, 
  Warning, 
  Promotion, 
  Refresh, 
  Download 
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

export default {
  name: 'ResultCard',
  components: {
    TrendCharts,
    Key,
    TrophyBase,
    Warning,
    Promotion,
    Refresh,
    Download
  },
  props: {
    result: {
      type: Object,
      required: true,
      default: () => ({
        keywords: [],
        strengths: '',
        weaknesses: '',
        suggestions: '',
        matchScore: 0
      })
    }
  },
  emits: ['analyze-again'],
  setup(props) {
    // 添加调试日志
    console.log('ResultCard props:', props.result)
    console.log('matchScore value:', props.result?.matchScore)
    console.log('matchScore type:', typeof props.result?.matchScore)
    
    return {}
  },
  methods: {
    /**
     * 根据分数获取进度条颜色
     */
    getScoreColor(score) {
      if (score >= 80) return '#67c23a'  // 绿色
      if (score >= 60) return '#e6a23c'  // 橙色
      if (score >= 40) return '#f56c6c'  // 红色
      return '#909399'  // 灰色
    },

    /**
     * 根据分数获取文字颜色
     */
    getScoreTextColor(score) {
      if (score >= 80) return '#67c23a'  // 绿色
      if (score >= 60) return '#e6a23c'  // 橙色
      if (score >= 40) return '#f56c6c'  // 红色
      return '#909399'  // 灰色
    },

    /**
     * 导出分析结果
     */
    exportResult() {
      try {
        const content = this.generateExportContent()
        const blob = new Blob([content], { type: 'text/plain;charset=utf-8' })
        const url = URL.createObjectURL(blob)
        
        const link = document.createElement('a')
        link.href = url
        link.download = `简历分析结果_${new Date().toLocaleDateString()}.txt`
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        
        URL.revokeObjectURL(url)
        ElMessage.success('结果导出成功')
      } catch (error) {
        console.error('导出失败:', error)
        ElMessage.error('导出失败，请稍后重试')
      }
    },

    /**
     * 生成导出内容
     */
    generateExportContent() {
      const { keywords, strengths, weaknesses, suggestions, matchScore } = this.result
      const date = new Date().toLocaleString()
      
      return `SmartResume-AI 简历分析报告
生成时间: ${date}

职业匹配度: ${matchScore || 0}分

技能关键词:
${keywords && keywords.length > 0 ? keywords.join(', ') : '无'}

优势分析:
${strengths || '无'}

待改进项:
${weaknesses || '无'}

优化建议:
${suggestions || '无'}

---
本报告由 SmartResume-AI 智能简历分析工具生成
技术支持: Vue3 + Element Plus + Spring Boot + Spark Lite`
    }
  }
}
</script>

<style scoped>
.result-container {
  margin-top: 30px;
  animation: fadeInUp 0.6s ease-out;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.result-header {
  display: flex;
  align-items: center;
  margin-bottom: 25px;
  padding: 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  color: white;
  box-shadow: 0 4px 20px rgba(102, 126, 234, 0.3);
}

.header-icon {
  font-size: 28px;
  margin-right: 12px;
}

.result-header h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  flex: 1;
}

.match-score {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  min-width: 200px;
}

.score-label {
  font-size: 14px;
  margin-bottom: 8px;
  opacity: 0.9;
}

.score-display {
  margin-bottom: 8px;
}

.score-number {
  font-size: 32px;
  font-weight: bold;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.result-content {
  margin-bottom: 30px;
}

.result-card {
  height: 280px;
  margin-bottom: 20px;
  border-radius: 12px;
  transition: all 0.3s ease;
  border: none;
}

.result-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.card-header {
  display: flex;
  align-items: center;
  font-weight: 600;
  font-size: 16px;
}

.card-icon {
  font-size: 20px;
  margin-right: 8px;
}

.keywords-card .card-header {
  color: #409eff;
}

.strengths-card .card-header {
  color: #67c23a;
}

.weaknesses-card .card-header {
  color: #e6a23c;
}

.suggestions-card .card-header {
  color: #f56c6c;
}

.keywords-content {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  max-height: 180px;
  overflow-y: auto;
}

.keyword-tag {
  margin: 2px;
  font-size: 13px;
  border-radius: 16px;
  padding: 4px 12px;
}

.content-text {
  line-height: 1.8;
  color: #606266;
  font-size: 14px;
  max-height: 180px;
  overflow-y: auto;
}

.content-text p {
  margin: 0;
  word-break: break-word;
}

.empty-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 120px;
  color: #909399;
  font-size: 14px;
}

.empty-content .el-icon {
  font-size: 32px;
  margin-bottom: 8px;
}

.result-actions {
  text-align: center;
  padding: 20px;
  background-color: #fafafa;
  border-radius: 12px;
}

.result-actions .el-button {
  margin: 0 10px;
  padding: 12px 24px;
  border-radius: 8px;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .result-content .el-col {
    margin-bottom: 15px;
  }
  
  .result-card {
    height: auto;
    min-height: 220px;
  }
}

@media (max-width: 768px) {
  .result-header {
    flex-direction: column;
    text-align: center;
    padding: 15px;
  }
  
  .result-header h2 {
    font-size: 20px;
    margin-bottom: 15px;
  }
  
  .header-icon {
    font-size: 24px;
  }
  
  .match-score {
    align-items: center;
    margin-top: 0;
    min-width: auto;
    width: 100%;
    max-width: 300px;
    margin: 0 auto;
  }
  
  .score-number {
    font-size: 28px;
  }
  
  .result-content {
    margin: 0 -10px;
  }
  
  .result-content .el-col {
    padding: 0 10px;
    margin-bottom: 20px;
  }
  
  .result-card {
    height: auto;
    min-height: 180px;
  }
  
  .card-header {
    font-size: 15px;
  }
  
  .card-icon {
    font-size: 18px;
  }
  
  .keywords-content {
    max-height: 150px;
  }
  
  .keyword-tag {
    font-size: 12px;
    padding: 3px 10px;
  }
  
  .content-text {
    font-size: 13px;
    line-height: 1.6;
    max-height: 150px;
  }
  
  .result-actions {
    padding: 15px;
    margin: 0 -10px;
  }
  
  .result-actions .el-button {
    margin: 5px;
    width: 100%;
    max-width: 200px;
    font-size: 14px;
    padding: 10px 20px;
  }
}

@media (max-width: 480px) {
  .result-header {
    padding: 12px;
  }
  
  .result-header h2 {
    font-size: 18px;
  }
  
  .header-icon {
    font-size: 20px;
  }
  
  .score-number {
    font-size: 24px;
  }
  
  .score-label {
    font-size: 12px;
  }
  
  .result-content {
    margin: 0 -5px;
  }
  
  .result-content .el-col {
    padding: 0 5px;
    margin-bottom: 15px;
  }
  
  .result-card {
    min-height: 160px;
  }
  
  .card-header {
    font-size: 14px;
  }
  
  .card-icon {
    font-size: 16px;
  }
  
  .keywords-content {
    max-height: 120px;
    gap: 6px;
  }
  
  .keyword-tag {
    font-size: 11px;
    padding: 2px 8px;
  }
  
  .content-text {
    font-size: 12px;
    max-height: 120px;
  }
  
  .result-actions {
    padding: 12px;
  }
  
  .result-actions .el-button {
    width: 100%;
    max-width: none;
    margin: 3px 0;
    font-size: 13px;
    padding: 8px 16px;
  }
}

/* 滚动条样式 */
.keywords-content::-webkit-scrollbar,
.content-text::-webkit-scrollbar {
  width: 4px;
}

.keywords-content::-webkit-scrollbar-track,
.content-text::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 2px;
}

.keywords-content::-webkit-scrollbar-thumb,
.content-text::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 2px;
}

.keywords-content::-webkit-scrollbar-thumb:hover,
.content-text::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}
</style>